package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Activity;
import com.qk.enums.ParamEnum;
import com.qk.exception.CommonException;
import com.qk.management.mapper.ActivityMapper;
import com.qk.management.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 *
 * @author lyl
 * @created 2025/10/9 9:05
 * @description
 *
 */
@Service
@SuppressWarnings("all")
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public PageResult<Activity> getListByPage(Integer channel, Integer type,Integer status, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Activity> list = activityMapper.getListByPage(channel, type, status);
        Page<Activity> pageResult = (Page<Activity>)  list;
        return PageResult.<Activity>builder()
                .total(Long.valueOf(pageResult.getTotal()).intValue())
                .rows(pageResult.getResult())
                .build();
    }

    @Override
    public void add(Activity activity) {
        boolean hasNull = BeanUtil.hasNullField(activity, "id","discount", "voucher", "createTime", "updateTime");
        if (hasNull) {
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.add(activity);
    }

    @Override
    public void delete(Integer id) {
        activityMapper.delete(id);
    }

    @Override
    public Activity getActivityById(Integer id) {
        return activityMapper.getActivityById(id);
    }

    @Override
    public void update(Activity activity) {
        boolean hasNull = BeanUtil.hasNullField(activity, "discount", "voucher", "updateTime");
        if (hasNull) {
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.update(activity);
    }

    @Override
    public List<Activity> getListByType(Integer type) {
        return activityMapper.getListByType(type);
    }
}
